from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from fedexweb.pud.models import *


def index( request ):
    puds = Delivery.objects.all()
    return render_to_response( 'index.html', {'puds': puds} )


def detail( request, id ):
    if request.method == 'GET':
        pud = get_object_or_404( Delivery, id=id )
        form = DeliveryForm( instance=pud )
    elif request.method == 'POST':
        form = DeliveryForm( request.POST )
        if form.is_valid():
            form.save()
            # Redirect if the form is valid
            return HttpResponseRedirect( '/pud/%d/label' % id )

    # Return either an empty form or a form with errors
    return render_to_response( 'detail.html', {'form': form} )


def label( request, id ):
    pud = get_object_or_404( Delivery, id=id )
    return render_to_response( 'label.html', {'pud': pud} )


